<template>
    <view class="pub_page pub_realname pub_header_padding">
        <pub-head headerName="realname" :headerConfig="headerConfig"></pub-head>
        <div class="pub_page_scroll page_box" v-if="userInfo">
            <div class="pub_inputs">
                <!-- <div class="abox">
                    <div class="ali">
                        <div class="atitle">微信号</div>
                        <input class="ainput" type="text" placeholder="请输入微信号" v-model="contactWx">
                    </div>
                    <div class="ali">
                        <div class="atitle">QQ号</div>
                        <input class="ainput" type="text" placeholder="请输入QQ号" v-model="contactQq">
                    </div>
                </div>
                <br>
                <br>
                <div class="pub_btn" @click="toSave(1)">确认绑定</div>
                <br>
                <br>
                <br> -->
                <div class="abox">
                    <div class="ali">
                        <div class="atitle">手机号</div>
                        <input class="ainput" type="text" placeholder="请输入手机号" :value="userInfo.mobile"
                            :disabled="true">
                    </div>
                    <div class="ali">
                        <div class="atitle">微信号</div>
                        <input class="ainput" type="text" placeholder="请输入微信号" v-model="contactWx">
                    </div>
                    <div class="ali">
                        <div class="atitle">QQ号</div>
                        <input class="ainput" type="text" placeholder="请输入QQ号" v-model="contactQq">
                    </div>
                    <div class="ali">
                        <div class="atitle">姓名</div>
                        <input class="ainput" type="text" placeholder="请输入姓名" v-model="name"
                            :disabled="userInfo.cardName ? true : false">
                    </div>
                    <div class="ali">
                        <div class="atitle">身份证号</div>
                        <input class="ainput" type="text" placeholder="请输入身份证号" v-model="idCard"
                            :disabled="userInfo.cardName ? true : false">
                    </div>
                </div>
                <br>
                <br>
                <!-- <div class="pub_nodata_2" v-if="!userInfo.cardName" style="color:#ff0000">请记住认证信息，修改密码将需要</div>
                <div class="pub_btn" v-if="!userInfo.cardName" @click="toSave(3)">实名认证</div> -->
                <div class="pub_btn" @click="toSave(4)">保存</div>
            </div>
        </div>
    </view>
</template>

<script>
export default {
    data() {
        return {
            pagescrollTop: 0,
            headerConfig: {
                title: '实名认证',//标题栏内容
                left_type: 3,
                title_color: '#000',
                left_content: 'dark',
                header_color: '#fff'
            },
            userInfo: '',

            idCard: "",
            name: "",

            contactQq: "",
            contactWx: "",
        };
    },
    onLoad() {
        this.getInfo();
    },
    onPageScroll(res) {
        this.pagescrollTop = res.scrollTop;
    },
    methods: {
        async getInfo() {
            this.$comFun.openLoading();
            const res = await this.$request.get('user/userInfo', {});
            // console.log(res);
            this.$comFun.closeLoading();
            if (res.code == 200) {
                this.userInfo = res.result;

                this.idCard = res.result.cardId || '';
                this.name = res.result.cardName || '';
                this.contactQq = res.result.contactQq || '';
                this.contactWx = res.result.contactWx || '';
            }
        },
        async toSave(type) {
            if (type == 1) {
                if (!this.contactWx || !this.contactQq) {
                    this.$comFun.toast('请将账号信息填写完整');
                    return;
                }
                this.$comFun.openLoading();
                const res = await this.$request.post('user/updateInfo', {
                    contactQq: this.contactQq,
                    contactWx: this.contactWx,
                });
                // console.log(res);
                this.$comFun.closeLoading();
                if (res.code == 200) {
                    this.$comFun.toast('绑定成功');
                } else {
                    this.$comFun.toast(res.message);
                }
            } else if (type == 2) {
                if (!this.idCard || !this.name) {
                    this.$comFun.toast('请将身份信息填写完整');
                    return;
                }
                const APFaceDetectModule = uni.requireNativePlugin('AP-FaceDetectModule');
                // console.log(APFaceDetectModule);
                let metaInfo = APFaceDetectModule.getMetaInfo();
                if (this.$store.state.deviceType == 'ios') {
                    metaInfo = JSON.stringify(metaInfo);
                }
                // console.log(metaInfo);
                // console.log({
                //     idCard: this.idCard,
                //     name: this.name,
                //     metaInfo: metaInfo
                // });
                this.$comFun.openLoading();
                const res = await this.$request.post('user/getFaceVerifyCertifyId', {
                    idCard: this.idCard,
                    name: this.name,
                    metaInfo: metaInfo
                });
                // console.log(res);
                if (res.code == 200) {
                    const that = this;
                    const AliCertifyId = res.result;
                    APFaceDetectModule.verify({
                        certifyId: AliCertifyId,
                        ext_params_key_use_video: false,
                        returnVideo: false,
                        ext_params_key_ocr_bottom_button_clicked_color: '#037759',
                        ext_params_key_ocr_face_circle_color: '#037759',
                    }, function (bres) {
                        // console.log(bres);
                        if (bres.code == 1000 || bres.retCode == 1000) {
                            setTimeout(async () => {
                                const res_real = await that.$request.get('user/getFaceVerify', {
                                    certifyId: AliCertifyId
                                });
                                // console.log(res_real);
                                that.$comFun.closeLoading();
                                if (res_real.code == 200) {
                                    that.getInfo();
                                    that.$comFun.toast('认证成功');
                                } else {
                                    that.$comFun.toast(res_real.message);
                                }
                            }, 1000)
                        } else {
                            that.$comFun.closeLoading();
                            that.$comFun.toast('认证失败，请重试');
                        }
                    })
                } else {
                    this.$comFun.closeLoading();
                    this.$comFun.toast(res.message);
                }
            } else if (type == 3) {
                if (!this.idCard || !this.name) {
                    this.$comFun.toast('请将身份信息填写完整');
                    return;
                }

                this.$comFun.openLoading();
                const res = await this.$request.post('user/verify', {
                    idCard: this.idCard,
                    name: this.name,
                    metaInfo: '1'
                });
                // console.log(res);
                this.$comFun.closeLoading();
                if (res.code == 200) {
                    this.getInfo();
                    this.$comFun.toast('认证成功');
                } else {
                    this.$comFun.toast(res.message);
                }
            } else if (type == 4) {
                this.$comFun.toast('敬请期待');
            }
        }
    },
};
</script>

<style lang="scss" scoped>
@import "@/common/css/mine.scss";
</style>
